package com.blb.controller;

import com.blb.dto.*;
import com.blb.service.ZLevelService;
import com.blb.service.ZRealnameService;
import com.blb.service.ZUserService;
import com.blb.service.ZZhuboService;
import com.blb.utils.LayUIDataTable;
import com.blb.utils.Status;
import com.blb.vo.Guanzhulist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
@CrossOrigin
public class UserController {

    @Autowired
    private ZUserService zUserService;

    @Autowired
    private ZLevelService zLevelService;

    @Autowired
    private ZZhuboService zZhuboService;

    @Autowired
    private ZRealnameService zRealnameService;

    @PostMapping("/shiming/{uid}/{usernum}/{realname}")
    @ResponseBody
    public LayUIDataTable shiming(@PathVariable("uid") int uid,@PathVariable("usernum")String userunm,@PathVariable("realname")String realname){
        LayUIDataTable dt = new LayUIDataTable();
        ZUser zUser = zUserService.selectByPrimaryKey(uid);
        zUser.setIsrealname(0+"");
        zUserService.updateByPrimaryKey(zUser);
        ZRealname zRealname = new ZRealname();
        zRealname.setUid(uid);
        zRealname.setIdnum(userunm);
        zRealname.setTruename(realname);
        int insert = zRealnameService.insert(zRealname);
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("认证成功！");
        dt.setData(insert);
        return dt;
    }


    @RequestMapping("/showguanzhu/{uid}")
    @ResponseBody
    public LayUIDataTable queryguanzhu(Model model,@PathVariable("uid") int uid)
    {
        LayUIDataTable dt = new LayUIDataTable();
        ZUser zUser = zUserService.selectByPrimaryKey(uid);
        String concernlist = zUser.getConcernlist();
        List<Guanzhulist> arryList = new ArrayList<>();
        if (concernlist==null||concernlist.equals("null")||concernlist=="")
        {
            dt.setMsg("暂无关注！");
            dt.setData("");
        }
        else{
            String[] split = concernlist.split("\\$");
            for (int j=0;j<split.length;j++)
            {
                if (split[j]!=""){
                    ZZhubo zhubo = zZhuboService.selectByPrimaryKey(Integer.parseInt(split[j].trim()));
                    ZUser zUser1 = zUserService.selectByPrimaryKey(zhubo.getUid());
                    Guanzhulist guanzhulist = new Guanzhulist();
                    guanzhulist.setZid(Integer.parseInt(split[j].trim()));
                    guanzhulist.setPic(zUser1.getUpicture());
                    guanzhulist.setNichen(zUser1.getNichen());
                    guanzhulist.setTitle(zhubo.getTitle());
                    arryList.add(guanzhulist);
                }
            }
            dt.setMsg("我关注的主播");
            dt.setData(arryList);
        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        return dt;
    }

    @RequestMapping("/{uid}")
    @ResponseBody
    public LayUIDataTable querybyid(Model model,@PathVariable("uid") int uid)
    {
        ZUser zUser = zUserService.selectByPrimaryKey(uid);
        Integer experience = zUser.getExperience();   //根据经验判断用户等级
        ZLevelExample zLevelExample = new ZLevelExample();
        ZLevelExample.Criteria criteria = zLevelExample.createCriteria();
        List<ZLevel> zLevels = zLevelService.selectByExample(zLevelExample);
        int lv = 1;//用户等级
        String nextexe = "";//距离下一个等级还差多少经验
        for (ZLevel z:zLevels) {
            if(z.getNeedexp()<experience){
                lv=z.getLevel();
            }
            else{
                zUser.setExperience(lv);
                break;
            }

        }
        if(experience>=(zLevels.get(zLevels.size()-1).getNeedexp())){
            nextexe="您已经满级";
        }
        else{
            nextexe = zLevelService.selectByPrimaryKey(lv + 1).getNeedexp() - experience+"";
        }
        LayUIDataTable dt = new LayUIDataTable();
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("查询成功！");
        dt.setData(zUser);
        dt.setData2(nextexe);
        return dt;
    }

    @RequestMapping("/isguanzhu/{uid}/{zid}")
    @ResponseBody
    public LayUIDataTable guanzhu(Model model,@PathVariable("uid") String uid,@PathVariable("zid") int zid)
    {
        LayUIDataTable dt = new LayUIDataTable();
        dt.setData(1);
        if(!uid.equals("null")){
            ZUser zUser = zUserService.selectByPrimaryKey(Integer.parseInt(uid));
            String concernlist = zUser.getConcernlist();
            if(concernlist==null||concernlist=="")
            {
                dt.setData(1);
            }
            else{
                String[] split = concernlist.split("\\$");
                for (int j=0;j<split.length;j++){
                    if (split[j].equals(zid+"")){
                        dt.setData(0);
                        break;
                    }
                }
            }

        }
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("查询成功！");
        return dt;
    }

    @GetMapping("guanzhu/{uid}/{zid}")
    @ResponseBody
    public LayUIDataTable guanzhuclick(Model model,@PathVariable("uid") int uid,@PathVariable("zid") int zid){
        LayUIDataTable dt = new LayUIDataTable();
        ZZhubo zhubo = zZhuboService.selectByPrimaryKey(zid);
        Integer guanzhunum = zhubo.getGuanzhunum()+1;
        zhubo.setGuanzhunum(guanzhunum);
        zZhuboService.updateByPrimaryKey(zhubo);
        ZUser zUser = zUserService.selectByPrimaryKey(uid);
        String concernlist = zUser.getConcernlist();
        if (concernlist==null||concernlist.equals("null")||concernlist=="")
        {
            concernlist=zid+"$";
        }
        else {
            concernlist=concernlist+zid+"$";
        }
        zUser.setConcernlist(concernlist);
        zUserService.updateByPrimaryKey(zUser);
        dt.setData(0);
        dt.setData2(zhubo);
        dt.setCode(Status.SUCCESS);
        dt.setCount(0);
        dt.setMsg("关注成功！");
        return dt;
    }

    @PostMapping("/login/{username}/{password}")
    @ResponseBody
    public LayUIDataTable userlogin(Model model,@PathVariable("username") String username,@PathVariable("password") String password) {
        ZUserExample zUserExample = new ZUserExample();
        ZUserExample.Criteria criteria =zUserExample.createCriteria();
        criteria.andUnameEqualTo(username);
        criteria.andUpwdEqualTo(password);
        List<ZUser> zUsers = zUserService.selectByExample(zUserExample);
        LayUIDataTable dt = new LayUIDataTable();
        if(zUsers.size()!=0){
            dt.setCode(Status.SUCCESS);
            dt.setCount(1);
            dt.setMsg("登录成功！");
            dt.setData(zUsers.get(0).getUid());
            dt.setData2(zUsers.get(0).getIsrealname());
            dt.setData3(zUsers.get(0).getIszhubo());
        }
        else{
            dt.setCode(Status.FAIL);
            dt.setCount(0);
            dt.setMsg("账号不存在或密码错误！");
            dt.setData(1);
        }
        return dt;
    }

    @PostMapping("/zhuce")
    @ResponseBody
    public LayUIDataTable userzhuce(Model model,@RequestBody ZUser zUser){
        LayUIDataTable dt = new LayUIDataTable();
        ZUserExample zUserExample = new ZUserExample();
        ZUserExample.Criteria criteria =zUserExample.createCriteria();
        criteria.andUnameEqualTo(zUser.getUname());
        List<ZUser> zUsers = zUserService.selectByExample(zUserExample);
        if(zUsers.size()!=0)
        {
            dt.setCode(Status.FAIL);
            dt.setCount(1);
            dt.setMsg("该用户账号已被注册！请更换用户名*_*");
            dt.setData(2);
        }else{
            if (zUser.getUpicture()==null||zUser.getUpicture()==""||zUser.getUpicture()=="null"){
                zUser.setUpicture("img/touxiang.jpg");
            }
            zUser.setLv(1);
            zUser.setIsrealname(1+"");
            zUser.setIszhubo(1+"");
            zUser.setMoneybag(new BigDecimal(0));
            zUser.setExperience(0);
            int insert = zUserService.insert(zUser);
            if(insert>0)
            {
                dt.setCode(Status.SUCCESS);
                dt.setCount(1);
                dt.setMsg("注册成功！");
                dt.setData(0);
            }
            else{
                dt.setCode(Status.FAIL);
                dt.setCount(1);
                dt.setMsg("注册失败！");
                dt.setData(1);
            }
        }
        return dt;
    }


    @PostMapping("update/{type}/{value}/{uid}")
    @ResponseBody
    public  LayUIDataTable update(Model model,@PathVariable("type") int type,@PathVariable("value") String value,@PathVariable("uid") int uid){
        LayUIDataTable dt = new LayUIDataTable();
        System.out.println(value);
        ZUser zUser = zUserService.selectByPrimaryKey(uid);
        switch (type){
            case 0:{
                zUser.setNichen(value);
                break;
            }
            case 1:{
                System.out.println("充值");
                BigDecimal moneybag = zUser.getMoneybag();
                BigDecimal bigDecimal = new BigDecimal(value);
                BigDecimal add = moneybag.add(bigDecimal);
                zUser.setMoneybag(add);
                System.out.println("充值成功");
                break;
            }
            case 2:{
                zUser.setPhonenumber(value);
                break;
            }
            case 3:{
                zUser.setSex(value);
                break;
            }
        }
        zUserService.updateByPrimaryKey(zUser);
        dt.setCode(Status.SUCCESS);
        dt.setCount(1);
        dt.setMsg("修改成功！");
        dt.setData(1);
        return dt;
    }
}
